package com.lccviolet;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author LiChaochao
 * @Date 2021/9/10 19:01
 * @Description BlockingQueueDemo 阻塞队列
 */
public class BlockingQueueDemo {

    public static void main(String[] args) throws InterruptedException {
        // 需要手动指定大小
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

        // 抛异常型：执行不成功就抛异常
        blockingQueue.add("a");
        blockingQueue.add("b");
        blockingQueue.remove();
        blockingQueue.remove("b");

        // 返回布尔型：执行结果为true或false
        blockingQueue.offer("c");
        blockingQueue.poll();

        // 阻塞型：一直等待，直到执行为止
        blockingQueue.put("d");
        blockingQueue.take();

        // 超时型：当操作不成功时，只等待一段时间，并结束
        blockingQueue.offer("e", 4, TimeUnit.SECONDS);
        blockingQueue.poll(2, TimeUnit.SECONDS);
    }
}
